Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution

First a list with the multipliers of 3 below 1000 are calculated


In [18]:
limit = 1000
m3 = [3 * i  for i in range(limit/3 + 1) if i > 0 and 3 * i < limit]

Another list with the multipliers of 5 below 5


In [19]:
m5 = [5 * i for i in range(limit/5 + 1) if i > 0 and 5 * i < limit]

The two list are joint and converted into a set so to avoid repeated numbers (for example, 15 es a multiplier of 3 and 5)


In [20]:
l = set(m3 + m5)

Sum all the elements


In [21]:
sum = 0
for e in l:
    sum += e

Print the solution to the problem


In [29]:
print("The solution to the problem is {}").format(sum)


The solution to the problem is 233168

In [28]: